package cn.wit.sst.service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.wit.sst.dto.CollegeQuery;
import cn.wit.sst.dto.ScoreRankDto;
import cn.wit.sst.mapper.ScoreMapper;
import cn.wit.sst.utils.Utils;

@Service
@Transactional
public class CollegeService {
    @Autowired
    ScoreMapper scoreMapper;

    public Map<Long, List<ScoreRankDto>> search(CollegeQuery query) {
        int examYear = query.getGrade() + query.getYear();

        List<ScoreRankDto> scores = scoreMapper.
                getScoreRanksForCollege(query.getYear(),
                        examYear,
                        query.getSemester());

        // 将各课程分数分组		（key为课程id，value为所有学生该课程的分数集合）
        Map<Long, List<ScoreRankDto>> map = scores.stream()
                .collect(Collectors.groupingBy(ScoreRankDto::getCid));

        // 遍历每一学科，计算排名
        map.forEach((key, value) -> Utils.computeRank(value));

        return map;
    }
}
